package com.woniu.woniuproject.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.woniuproject.config.SendSms;
import com.woniu.woniuproject.config.SendSms2;
import com.woniu.woniuproject.entity.Debt_info;
import com.woniu.woniuproject.entity.Debtor;
import com.woniu.woniuproject.exceptions.PhoneExption;
import com.woniu.woniuproject.service.Debt_infoService;
import com.woniu.woniuproject.service.DebtorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("debtor")
public class DebtorController {
    @Autowired
    DebtorService debtorService;
    @Autowired
    Debt_infoService debtInfoService;
    @Autowired
    SendSms2 send;
    @Autowired
    SendSms sendSms;

    @RequestMapping("/apply")
    public String apply(Debtor debtor, Map map) throws ParseException {
        if (debtor == null) {
            return "redirect:/borrow.html";
        }
        System.out.println(debtor.getDebtorinfo());
        System.out.println(debtor.getDebtortype());
        debtor.setDebtorstatus(0);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        Date date=calendar.getTime();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        Date date1 = sdf.parse(format);
        debtor.setDebtordate(date1);
        int i = debtorService.adddebtor(debtor);
        if (i == 1) {
            map.put("msg", "申请提交成功，请耐心等待");
        } else {
            map.put("msg", "申请提交失败，请重新提交");
            return "borrow.html";
        }

        return "borrow.html";

    }


    @ResponseBody
    @GetMapping("/list")
    public Map<String,PageInfo> list(@RequestParam(value="currentPage",defaultValue="1")int currentPage,
                                   @RequestParam(value="pageSize",defaultValue="5")int pageSize,
                                   @RequestParam (value="startTime",defaultValue="1900-01-01")String startTime,@RequestParam (value="endTime",defaultValue="2099-12-31")String endTime) throws ParseException {
              HashMap<String, PageInfo> map=new HashMap<>();
              SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");


              Date start = sdf.parse(startTime);
              Date end = sdf.parse(endTime);
              PageInfo<Debtor> pageInfo = debtorService.selectApplyList(currentPage, pageSize, start, end);
              map.put("data",pageInfo);

        return map;
    }


    @ResponseBody
    @GetMapping("/modify")
    public Map<String,String> update(@RequestParam(value="debtorid",defaultValue="0")int debtorid){
        HashMap<String, String> map=new HashMap<>();
        System.out.println(debtorid);
        int i=debtorService.updateById(debtorid);
        if(i==1){
            map.put("data","1");
        }else{
            map.put("data","0");
        }
        Debtor debtor = debtorService.selectById(debtorid);
        Debt_info debtorInfo=new Debt_info();
        debtorInfo.setDebtorsname(debtor.getDebtorname());
        debtorInfo.setLoanstartdate(new Date());
        debtorInfo.setDebtmoney(debtor.getDebtormoney());
        debtorInfo.setDebtstatus("未匹配");
        debtorInfo.setDebttype("贷款");
        debtorInfo.setRepaymentstyle(debtor.getDebtortype());
        String a=String.valueOf(debtor.getDebtordays());
        debtorInfo.setLoanperiod(a);
        debtorInfo.setDebtyearrate(16.2);
        debtorInfo.setLoanperiod(String.valueOf(debtor.getDebtordays()));
        System.out.println(debtor.getDebtorstyle());
        debtorInfo.setRepaymentstylename(debtor.getDebtorstyle());
        System.out.println(debtorInfo);
        debtInfoService.insert(debtorInfo);
        return map;
    }


    @ResponseBody
    @GetMapping("/unmodify")
    public Map<String,String> unupdate(@RequestParam(value="debtorid",defaultValue="0")int debtorid){
        HashMap<String, String> map=new HashMap<>();
        System.out.println(debtorid);
        Debtor debtor = debtorService.selectById(debtorid);
        //生成验证码
        String code= "您申请的贷款审核未通过，请及时重新申请";
        HashMap<String,Object> param=new HashMap<>();
        param.put("code",code);
        //使用验证码
        Map isSend=sendSms.send(debtor.getDebtorphone(),"SMS_205464231",param);
        if((Boolean) isSend.get("boolean")){
            System.out.println("发送成功");
        }else{
            throw new PhoneExption("验证码发送失败");
        }
        int i=debtorService.updateByunId(debtorid);
        if(i==1){
            map.put("data","1");
        }else{
            map.put("data","0");
        }
        return map;
    }

}